﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Text;

namespace Asion.Dapper
{

    public static class ComplexSqlHelper
    {

        public static ResultA Query<ResultA>(string sql, object param = null)
        {
            var result = default(ResultA);
            var name = TenantMananger.GetMasterTenantsName();
            var factory = DapperConfiguration.connection_factory;
            using (var connection = factory.CreateConnection(name))
            {
                result = connection.QueryFirst<ResultA>(sql, param);
            }
            return result;

        }


        /// <summary>
        /// query
        /// </summary>
        /// <typeparam name="ResultA"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static IEnumerable<ResultA> QueryList<ResultA>(string sql, object param = null)
        {
            var result = default(IEnumerable<ResultA>);
            var name = TenantMananger.GetMasterTenantsName();
            var factory = DapperConfiguration.connection_factory;
            using (var connection = factory.CreateConnection(name))
            {
                result = connection.Query<ResultA>(sql, param);
            }
            return result;

        }


        /// <summary>
        /// execute for insert and batch
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int Execute(string sql, object param = null)
        {
            var result = 0;
            var name = TenantMananger.GetMasterTenantsName();
            var factory = DapperConfiguration.connection_factory;
            using (var connection = factory.CreateConnection(name))
            {
                result = connection.Execute(sql, param);
            }
            return result;

        }
    }

}
